<html>
<head><meta charset="utf-8"><title>Non-panicking slice method alternatives · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html">Non-panicking slice method alternatives</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238532755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Non-panicking%20slice%20method%20alternatives/near/238532755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dirkjan Ochtman <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html#238532755">(May 12 2021 at 19:44)</a>:</h4>
<p>I've been working towards making rustls panic-free, and one thing that I looked at that seems tricky is that something like <code>slice::split_at()</code> and, in particular, <code>split_at_mut()</code> (since I think it's harder -- or even impossible -- to do in safe Rust), can panic. Presumably the optimizer might take care of that if <code>mid</code> is a constant, but I feel like it would be pretty nice if we had alternatives to these that would return <code>Option</code> or <code>Result</code>. What do people think?</p>



<a name="238533304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Non-panicking%20slice%20method%20alternatives/near/238533304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html#238533304">(May 12 2021 at 19:48)</a>:</h4>
<p>Where you'd never get <code>None</code> unless you passed an out-of-range index?</p>



<a name="238533651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Non-panicking%20slice%20method%20alternatives/near/238533651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dirkjan Ochtman <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html#238533651">(May 12 2021 at 19:51)</a>:</h4>
<p>Yes, exactly</p>



<a name="238533871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Non-panicking%20slice%20method%20alternatives/near/238533871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html#238533871">(May 12 2021 at 19:52)</a>:</h4>
<p>That seems reasonable, yeah.</p>



<a name="238534334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Non-panicking%20slice%20method%20alternatives/near/238534334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dirkjan Ochtman <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html#238534334">(May 12 2021 at 19:56)</a>:</h4>
<p>Feels a bit tricky because so many of slice's methods can panic, so there maybe needs to be a policy on what qualifies?<br>
Would the naming just be <code>try_split_at_mut()</code>?</p>



<a name="238534490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Non-panicking%20slice%20method%20alternatives/near/238534490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html#238534490">(May 12 2021 at 19:56)</a>:</h4>
<p>I'd honestly love to have non-panicking versions of <em>every</em> such function. We should probably have a naming convention.</p>



<a name="238534834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Non-panicking%20slice%20method%20alternatives/near/238534834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dirkjan Ochtman <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html#238534834">(May 12 2021 at 19:59)</a>:</h4>
<p>If that's the case, shouldn't unstable methods start being non-panicking from the start?</p>



<a name="238534868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Non-panicking%20slice%20method%20alternatives/near/238534868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dirkjan Ochtman <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html#238534868">(May 12 2021 at 19:59)</a>:</h4>
<p>(like <code>as_chunks()</code>, say)</p>



<a name="238534980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Non-panicking%20slice%20method%20alternatives/near/238534980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dirkjan Ochtman <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html#238534980">(May 12 2021 at 20:00)</a>:</h4>
<p>or is it okay at this level to just have two versions of every method?</p>



<a name="238545692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Non-panicking%20slice%20method%20alternatives/near/238545692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html#238545692">(May 12 2021 at 21:13)</a>:</h4>
<p>Like came up for <code>Vec</code>, could there be a <code>SlicePanicFree&lt;T&gt;</code>?  Because just a naming convention is vulnerable to "oops I forgot to use the <code>try_</code> version".  For example, I could easily imagine trying to make a quick PR to fix something and using the panicking version when they'd rather I didn't.</p>
<p>(That has its own problems of requiring duplicating the non-panicking things like <code>first()</code>, but maybe there's a nice way to share stuff...)</p>



<a name="238585032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Non-panicking%20slice%20method%20alternatives/near/238585032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dirkjan Ochtman <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Non-panicking.20slice.20method.20alternatives.html#238585032">(May 13 2021 at 06:01)</a>:</h4>
<p>Yeah, that does seem a better option</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>